home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / AppWannabe / Window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-11  |  20.5 KB  |  629 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Window.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19. /* This file contains the code for the document procedure pointers for the main Wannabe
  20. ** document.  Wannabe currently only supports one type of documents, type 'DUMD',
  21. ** which stands for "DUMb Document". */
  22.  
  23. /* For more information on this file, please read the read.me file "=How to write your app". */ 
  24.  
  25.  
  26.  
  27. /*****************************************************************************/
  28.  
  29.  
  30.  
  31. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  32. #include "App.defs.h"        /* Get various application definitions.            */
  33. #include "App.protos.h"        /* Get the prototypes for application.            */
  34.  
  35. #ifndef __ERRORS__
  36. #include <Errors.h>
  37. #endif
  38.  
  39. #ifndef __FONTS__
  40. #include <Fonts.h>
  41. #endif
  42.  
  43. #ifndef __RESOURCES__
  44. #include <Resources.h>
  45. #endif
  46.  
  47. #ifndef __TOOLUTILS__
  48. #include <ToolUtils.h>
  49. #endif
  50.  
  51. #ifndef __UTILITIES__
  52. #include "Utilities.h"
  53. #endif
  54.  
  55.  
  56.  
  57. /*****************************************************************************/
  58.  
  59.  
  60.  
  61. Boolean        gNoDefaultDocument = false;
  62.                     /* Set to true if app should boot with no default document. */
  63.                     /* This tells DTS.Lib..framework what you want. */
  64.  
  65. OSType        gAppWindowType = kDocFileType;    /* Main document type. */
  66. long        gAppWindowAttr = kwAppWindow;    /* Main window attributes. */
  67.  
  68. short        gMinVersion    = kMinVersion;    /* Minimum document version app can support. */
  69. short        gMaxVersion    = kMaxVersion;    /* Maximum document version app can support. */
  70.                                             /* More informing DTS.Lib..framework. */
  71.  
  72. extern short        gPrintPage;                /* Non-zero means we are printing. */
  73.                                             /* DTS.Lib..framework global. */
  74.  
  75. extern RgnHandle    gCursorRgn;                /* We handle cursors here, so we need */
  76. extern CursPtr        gCursorPtr;                /* to know about these things. */
  77.                                             /* Above are DTS.Lib..framework globals. */
  78.  
  79. /* Currently Wannabe doesn't ever change the cursor, so we don't actually need
  80. ** these referenced here.  However, since Wannabe is supposed to be an application
  81. ** in progress, it is very likely that you will need to reference these as your
  82. ** project develops.  See DTS.StyleChat and DTS.Draw for examples of setting the cursor. */
  83.  
  84. /* Some cursors are pointer-based, and some cursors are resource-based.
  85. ** If a cursor is resource-based, it needs to be loaded and made to not move,
  86. ** and then gCursorPtr can be set to point to it.  This makes all cursors
  87. ** pointer-based.  Also, gCursorPtr is used by DTS.Lib..framework to
  88. ** determine if there is a current cursor.  If gCursorPtr is nil, then
  89. ** there is no current cursor, and the cursor has to be recalculated, no
  90. ** matter where the mouse is.  If gCursorPtr is not nil, then if the
  91. ** mouse position is within the cursor region gCursorRgn, the cursor is
  92. ** correct, and no recalculation is necessary.  If it is outside this region,
  93. ** then it is recalculated.  What does this all mean?  It means that if you
  94. ** want to guarantee that the cursor is recalculated next time DoWindowCursor()
  95. ** is called, set gCursorPtr to nil.
  96. **
  97. ** If you have a cursor resource, you need to:
  98. ** 1) Load the resource.
  99. ** 2) Make a fixed copy of it.
  100. ** 3) Set the cursor to it.
  101. ** 4) Set gCursorPtr to point to the fixed copy.
  102. **
  103. ** There is a function that does almost all of this, called DoSetResCursor().
  104. ** It does all but set gCursorPtr to it.  (It actually sets gCursorPtr to nil.)
  105. ** It does return a pointer to the permanent copy, so typically what you will
  106. ** want to do is the following:
  107. **     gCursorPtr = DoSetResCursor(theCursorID);
  108. **
  109. ** So why set gCursorPtr to nil as the default action?  This allows you to
  110. ** set a temporary cursor, which will be replaced when DoWindowCursor() is
  111. ** called next, or it allows you to set a cursor that maps to the cursor
  112. ** region gCursorRgn (by setting gCursorPtr to the return result). */
  113.  
  114.  
  115.  
  116. /*****************************************************************************/
  117. /*****************************************************************************/
  118.  
  119.  
  120.  
  121. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  122.  
  123. /* Calculate application specific frame area (Called by DoCalcFrameRgn).
  124. ** You are passed an empty region.  You are supposed to add any custom frame
  125. ** parts that this document uses.  Typically there are no frame portions, as
  126. ** they are accounted for in other ways.  The scrollbars and grow icon will
  127. ** automatically be contributed to the calculation of the frame region.
  128. ** If you use sidebars, these are also added in automatically.  This is only
  129. ** used if the frame region is more complicated than can automatically be
  130. ** handled.  So, almost always, you will simply leave the region empty. */
  131.  
  132. #pragma segment TheDoc
  133. void    CalcFrameRgn(FileRecHndl frHndl, WindowPtr window, RgnHandle rgn)
  134. {
  135. #ifndef __MWERKS__
  136. #pragma unused (frHndl, window, rgn)
  137. #endif
  138. }
  139.  
  140.  
  141.  
  142. /*****************************************************************************/
  143.  
  144.  
  145.  
  146. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  147.  
  148. /* This is called (by DoContentClick()) when a mouse-down event occurs in the content of
  149. ** a window.  Other applications might want to call FindControl, TEClick, etc., to
  150. ** further process the click. */
  151.  
  152. #pragma segment TheDoc
  153. void    ContentClick(WindowPtr window, EventRecord *event, Boolean firstClick)
  154. {
  155. #ifndef __MWERKS__
  156. #pragma unused (firstClick)
  157. #endif
  158.  
  159.     ControlHandle    ctl;
  160.     short            action, cnum;
  161.  
  162.     cnum = IsCtlEvent(window, event, &ctl, &action);
  163.         /* That was easy.  Scrolling was just handled.  Other stuff would be handled
  164.         ** by IsCtlEvent, if we had other stuff to do.  We don't have any other
  165.         ** controls in the content besides the document scrollbars. */
  166.  
  167.     return;
  168. }
  169.  
  170.  
  171.  
  172. /*****************************************************************************/
  173.  
  174.  
  175.  
  176. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  177.  
  178. /* DoKeyDown() is first called by the application.  Then if the key isn't a menu
  179. ** key, DoKeyDown() calls this code.  Here are the rules for this function:
  180. **
  181. ** 1) If you handle the key, return(true).  This completes the key handling.
  182. ** 2) If you don't handle the key, you return false.  However, there are two
  183. **    situations for not handling the key:
  184. **      a) You want someone else to.
  185. **      b) You want nobody else to look at the key.
  186. **    This is what the boolean passThrough is for.  If you wish the next window
  187. **    to have a look at the key, set the boolean passThrough to true.  passThrough
  188. **    is already initialized to false, which is the common case, so you only have
  189. **    to worry about setting it true.
  190. **
  191. ** If you have a window that never processes keys and always passes them through,
  192. ** just set the contentKeyProc to nil.  This will indicate to the application
  193. ** framework that all keys should be passed through this window.  DTS.Draw has
  194. ** such a window.  Its palette window doesn't accept keys.  They are passed through
  195. ** to document windows. */
  196.  
  197. #pragma segment TheDoc
  198. Boolean    ContentKey(WindowPtr window, EventRecord *event, Boolean *passThrough)
  199. {
  200. #ifndef __MWERKS__
  201. #pragma unused (passThrough)
  202. #endif
  203.  
  204.     short    cnum;
  205.  
  206.     cnum = IsCtlEvent(window, event, nil, nil);
  207.         /* See DTS.Draw for an example of what you might do here. */
  208.  
  209.     return(true);
  210. }
  211.  
  212.  
  213.  
  214. /*****************************************************************************/
  215.  
  216.  
  217.  
  218. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  219.  
  220. /* Draw application specific content (Called by DoDrawFrame).
  221. **
  222. ** If your application has any custom frame areas, or if it uses sidebars,
  223. ** this is the function that you would put the frame drawing code.  The
  224. ** document scrollbars and grow icon drawing is handled by DTS.framework.
  225. ** Just do the sidebar and custom areas here. */
  226.  
  227. #pragma segment TheDoc
  228. void    DrawFrame(FileRecHndl frHndl, WindowPtr window, Boolean activate)
  229. {
  230.     MoveTo(0, (*frHndl)->fileState.topSidebar - 1);
  231.     LineTo((*frHndl)->fileState.leftSidebar - 1 - 16384, (*frHndl)->fileState.topSidebar - 1);
  232.     LineTo((*frHndl)->fileState.leftSidebar - 1 - 16384, 16383);
  233.  
  234.     BeginFrame(window);
  235.     DoDrawControls(window, activate);
  236.     EndFrame(window);
  237. }
  238.  
  239.  
  240.  
  241. /*****************************************************************************/
  242.  
  243.  
  244.  
  245. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  246.  
  247. /* Frees up any application-specific memory in the document.  This is called by
  248. ** DoFreeDocument, which is called by DisposeDocument().  The application would
  249. ** call DisposeDocument(), not DoFreeDocument() or FreeDocument() directly.
  250. **
  251. ** The document may have a bunch of handles off the main handle of the document.
  252. ** This is where they are freed.  DisposeDocument calls this prior to releasing
  253. ** the ram for the main handle of the document, so release everything else
  254. ** here, or you will have a memory leak.
  255. **
  256. ** NOTE:  Calling DefaultFreeDocument() frees up all memory used by a
  257. ** hierarchical document (see TreeObj package). */
  258.  
  259. #pragma segment TheDoc
  260. OSErr    FreeDocument(FileRecHndl frHndl)
  261. {
  262.     return(DefaultFreeDocument(frHndl));
  263. }
  264.  
  265.  
  266.  
  267. /*****************************************************************************/
  268.  
  269.  
  270.  
  271. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  272.  
  273. /* Any additional window disposal tasks can be handled here. */
  274.  
  275. #pragma segment TheDoc
  276. OSErr    FreeWindow(FileRecHndl frHndl, WindowPtr window)
  277. {
  278. #ifndef __MWERKS__
  279. #pragma unused (window)
  280. #endif
  281.  
  282.     WindowPtr    ww;
  283.     FileRecHndl    ff;
  284.  
  285.     if ((*frHndl)->fileState.sfType == kDocFileType) {
  286.         for (ww = nil; (ww = GetNextWindow(ww, 0)) != nil;) {
  287.             ff = (FileRecHndl)GetWRefCon(ww);
  288.             if ((*ff)->fileState.sfType == kViewHierFileType) {
  289.                 if ((*frHndl)->d.doc.root == (*ff)->d.doc.root) {
  290.                     DisposeOneWindow(ww, kClose);
  291.                     ww = nil;
  292.                 }
  293.             }
  294.         }
  295.     }
  296.  
  297.     return(noErr);
  298. }
  299.  
  300.  
  301.  
  302. /*****************************************************************************/
  303.  
  304.  
  305.  
  306. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  307.  
  308. /* Image the document into the current port.
  309. **
  310. ** The only thing tricky about this function is that it needs to key off of
  311. ** the global variable gPrintPage.  gPrintPage is the current page that is
  312. ** being printed.  If gPrintPage is 0, then you are drawing to the window.
  313. **
  314. ** For when printing:
  315. **
  316. ** If gPrintPage is non-0, that is the page to be printed.  If after imaging
  317. ** the page there are no more pages, you should set gPrintPage to 0.  This
  318. ** indicates to the print loop that the end of the document has been reached.
  319. ** Even if the user indicated in the job dialog to print more pages, setting
  320. ** gPrintPage to 0 states that the last page has been printed.  This is necessary
  321. ** because the print loop can't know when printing is done.  The imaging procedure
  322. ** is the logical one to state when everything has been imaged. */
  323.  
  324. #pragma segment TheDoc
  325. OSErr    ImageDocument(FileRecHndl frHndl)
  326. {
  327. #ifndef __MWERKS__
  328. #pragma unused (frHndl)
  329. #endif
  330.  
  331.     WindowPtr    curPort;
  332.  
  333.     GetPort(&curPort);
  334.     if (!gPrintPage) {                                    /* If not printing... */
  335.         DoDrawControls(curPort, false);                    /* Draw the content controls. */
  336.     }
  337.     gPrintPage = 0;
  338.  
  339.     return(noErr);
  340. }
  341.  
  342.  
  343.  
  344. /*****************************************************************************/
  345.  
  346.  
  347.  
  348. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  349.  
  350. /* This function does the remaining window initialization.
  351. **
  352. ** There may be additional content initialization for the window.  At this point,
  353. ** you have a window, but it is currently invisible.  If you return noErr, then
  354. ** the window will be set to the state indicated for that window.  Why this function?
  355. ** You may wish to add controls to the content of the window.  You may have a
  356. ** TextEdit record in the content.  All of these sort of things can't be created
  357. ** until there is a window to contain them.  First a document is read in, and then
  358. ** if the document creation succeeds, a window is created for that document.
  359. ** At this point we have a document, and we are on our way to having a window.
  360. ** All that remains is any additional content initialization.  Do it, return
  361. ** noErr, and everybody's happy.  If something goes wrong here, return the error,
  362. ** and the incomplete window will be disposed of. */
  363.  
  364. #pragma segment TheDoc
  365. OSErr    InitContent(FileRecHndl frHndl, WindowPtr window)
  366. {
  367.     OSErr    err;
  368.  
  369.     err = AddControlSet(window, (*frHndl)->fileState.sfType, kwStandardVis, 0, 0, nil);
  370.     return(err);
  371. }
  372.  
  373.  
  374.  
  375. /*****************************************************************************/
  376.  
  377.  
  378.  
  379. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  380.  
  381. /* The code below assumes that you are using the hierarchical document package.
  382. ** If you are, the entire hierarchical document is read in with just these two
  383. ** calls.  If you don't use it, you are on your own.  See DTS.StyleChat for an
  384. ** example of an application that uses the DTS.framework without the hierarchical
  385. ** document package. */
  386.  
  387. #pragma segment TheDoc
  388. OSErr    ReadDocument(FileRecHndl frHndl)
  389. {
  390.     OSErr    err;
  391.  
  392.     err = DefaultReadDocument(frHndl);
  393.     if (!err)
  394.         DefaultReadDocumentFixup(frHndl);
  395.  
  396.     return(err);
  397. }
  398.  
  399.  
  400.  
  401. /*****************************************************************************/
  402.  
  403.  
  404.  
  405. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  406.  
  407. /* Resize application specific content (Called by ResizeWindow).
  408. **
  409. ** This gets called when a user does a zoom or window resizing operation.
  410. ** It is possible that things in the content need to be resized in conjunction
  411. ** with the resizing of the window. */
  412.  
  413. #pragma segment TheDoc
  414. void    ResizeContent(WindowPtr window, short oldh, short oldv)
  415. {
  416. #ifndef __MWERKS__
  417. #pragma unused (window, oldh, oldv)
  418. #endif
  419.  
  420.     /* See DTS.StyleChat for a sample usage of this function. */
  421. }
  422.  
  423.  
  424.  
  425. /*****************************************************************************/
  426.  
  427.  
  428.  
  429. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  430.  
  431. /* Scroll application specific frame (Called by DoScrollFrame).
  432. **
  433. ** Some applications may need to scroll the "frame" of the document along
  434. ** with the document contents.  This is common for applications with rulers,
  435. ** or other similar sidebar items. */
  436.  
  437. #pragma segment TheDoc
  438. void    ScrollFrame(FileRecHndl frHndl, WindowPtr window, long dh, long dv)
  439. {
  440. #ifndef __MWERKS__
  441. #pragma unused (frHndl, window, dh, dv)
  442. #endif
  443. }
  444.  
  445.  
  446.  
  447. /*****************************************************************************/
  448.  
  449.  
  450.  
  451. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  452.  
  453. /* Since the hierarchical document package isn't used by DTS.StyleChat,
  454. ** this function actually never gets called. */
  455.  
  456. #pragma segment TheDoc
  457. void    UndoFixup(FileRecHndl frHndl, Point contOrg, Boolean afterUndo)
  458. {
  459. #ifndef __MWERKS__
  460. #pragma unused (frHndl, contOrg, afterUndo)
  461. #endif
  462.  
  463.     /* See DTS.Draw for an example of what you might do here. */
  464. }
  465.  
  466.  
  467.  
  468. /*****************************************************************************/
  469.  
  470.  
  471.  
  472. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  473.  
  474. /* This function is where you adjust the cursor to reflect the location in the
  475. ** document or window.  You have the additional input of gCursorRgn to deal
  476. ** with.  The way that the cursor handling works is as follows:
  477. ** 1) The application calls DoWindowCursor().
  478. ** 2) DoWindowCursor() works its way through the windows/documents, front to back.
  479. **    It looks at the document's windowCursorProc and checks to see if the document
  480. **    has one.  If the document doesn't have one, then it assumes that that window
  481. **    always wants an arrow.  If the cursor is over that window, the cursor is set
  482. **    to an arrow, and we're done.  If the cursor isn't over the window, then the next
  483. **    window is tried.  If all documents don't have a windowCursorProc, then the cursor
  484. **    is set to an arrow (for the non-document area of the screen).
  485. ** 3) If a document has a windowCursorProc, then the proc is called.  The proc's
  486. **    job is as follows:
  487. **    a) If the cursor is over a position that is determined by the window, then
  488. **       the proc removes other areas from gCursorRgn.  Note that it should not
  489. **       simply set the area to what it "thinks" is the correct area.  This window
  490. **       may not be the front-most.  Other windows will have already been subtracted
  491. **       from gCursorRgn.  The resultant gCursorRgn is the correct cursor area,
  492. **       and should be passed to WaitNextEvent calls in the application (already the case
  493. **       in EventLoop.c).  Also, the cursor should be set to the correct cursor, of course.
  494. **       You should also return true, as the cursor has been determined.
  495. **    b) If the cursor is not over a position for this window, then you should
  496. **       return.  You will either pass back true or false.  If you don't wish
  497. **       windows behind this window to have a shot at cursor determination, then
  498. **       return true.  This states that the cursor is "determined".  It is, in the
  499. **       sense that no further determination will occur.  If you return false, then
  500. **       other windows get a shot at determining the cursor.
  501. **
  502. ** Setting the cursor to the correct cursor isn't as easy as you would expect.
  503. ** DTS.Lib..framework uses the global gCursorPtr as the reference to the cursor.  This is
  504. ** fine if the cursor is pointer-based, but if the cursor is resource-based, it is a bit
  505. ** more of a problem.  What you will need to do is to call DoSetResCursor() to make the
  506. ** resource cursor pointer-based.  DoSetResCursor() will set gCursorPtr to nil, and it
  507. ** also returns the pointer to the permanent copy of the cursor resource.  Just set gCursorPtr
  508. ** to the return result of DoSetResCursor(), and you will be set. */
  509.  
  510. #pragma segment TheDoc
  511. Boolean    WindowCursor(FileRecHndl frHndl, WindowPtr window, Point globalPt)
  512. {
  513. #ifndef __MWERKS__
  514. #pragma unused (frHndl, window, globalPt)
  515. #endif
  516.  
  517.     /* For examples of applications that have non-arrow cursor regions,
  518.     ** see DTS.StyleChat and DTS.Draw. */
  519.  
  520.     DoSetCursor(&qd.arrow);
  521.     return(true);
  522. }
  523.  
  524.  
  525.  
  526. /*****************************************************************************/
  527.  
  528.  
  529.  
  530. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  531.  
  532. /* After the DTS.Lib framework disposes of a window, it calls here.  This is
  533. ** to give the application a chance to do any additional tasks related to
  534. ** a window closing.  DTS.StyleChat doesn't have anything else extra to do. */
  535.  
  536. #pragma segment TheDoc
  537. void    WindowGoneFixup(WindowPtr window)
  538. {
  539. #ifndef __MWERKS__
  540. #pragma unused (window)
  541. #endif
  542. }
  543.  
  544.  
  545.  
  546. /*****************************************************************************/
  547.  
  548.  
  549.  
  550. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  551.  
  552. /* The reverse function of ReadDocument. */
  553.  
  554. #pragma segment TheDoc
  555. OSErr    WriteDocument(FileRecHndl frHndl)
  556. {
  557.     return(DefaultWriteDocument(frHndl));
  558. }
  559.  
  560.  
  561.  
  562. /*****************************************************************************/
  563.  
  564.  
  565.  
  566. /* •• You don't call this.  DTS.Lib..framework does at open-application time. •• */
  567.  
  568. #pragma segment TheDoc
  569. OSErr    DoOpenApplication(void)
  570. {
  571.     return(noErr);
  572. }
  573.  
  574.  
  575.  
  576. /*****************************************************************************/
  577. /*****************************************************************************/
  578. /*****************************************************************************/
  579.  
  580.  
  581.  
  582. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  583.  
  584. #pragma segment TheDoc
  585. Boolean    AdjustMenuItems(WindowPtr window, short menuID)
  586. {
  587.     Boolean        redrawMenuBar;
  588.     MenuHandle    menu;
  589.  
  590.     redrawMenuBar = false;
  591.  
  592.     switch (menuID) {
  593.         case mFile:
  594.             redrawMenuBar = DoAdjustFileMenu(window);
  595.             break;
  596.         case mEdit:
  597.             redrawMenuBar = DoAdjustEditMenu(window);
  598.             break;
  599.         default:
  600.             menu = GetMenuHandle(menuID);
  601.             if (menu)
  602.                 (*menu)->enableFlags |= 0xFFFFFFFEL;
  603.             break;
  604.     }
  605.  
  606.     return(redrawMenuBar);
  607. }
  608.  
  609.  
  610.  
  611. /*****************************************************************************/
  612.  
  613.  
  614.  
  615. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  616.  
  617. #pragma segment TheDoc
  618. Boolean    DoMenuItem(WindowPtr window, short menuID, short menuItem)
  619. {
  620. #ifndef __MWERKS__
  621. #pragma unused (window)
  622. #endif
  623.  
  624.     return(DoMenuCommand(menuID, menuItem));
  625. }
  626.  
  627.  
  628.  
  629.